package com.chery.media.ui.play

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.chery.media.contacts.MusicPlayStatus
import com.chery.media.contacts.MusicType
import com.chery.media.library.base.BaseViewModel
import com.chery.media.library.event.SingleLiveEvent
import com.chery.media.music.BluetoothMusicTool
import com.chery.media.music.OperateMusicType
import com.chery.media.network.model.MusicBean
import com.chery.media.network.model.MusicPlayBean
import com.chery.media.service.MusicPlayService


val playViewModel: PlayViewModel by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    PlayViewModel()
}


class PlayViewModel: BaseViewModel() {

    // 当前播放的类型
    val currentType = SingleLiveEvent<MusicType>()

    // 当前播放的音乐
    val currentBluetoothPlayMusic = SingleLiveEvent<MusicPlayBean>()

    // 当前播放的音乐
    val currentUsbPlayMusic = SingleLiveEvent<MusicPlayBean>()

    // 当前播放的音乐
    val currentHotPlayMusic = SingleLiveEvent<MusicPlayBean>()

    // 音乐服务连接绑定
    var serviceConnection: ServiceConnection? = null

    @SuppressLint("StaticFieldLeak")
    var musicService: MusicPlayService? = null

    /**
     * 启动音乐服务
     */
    fun startService() {
        serviceConnection = object : ServiceConnection {
            override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                musicService = (service as MusicPlayService.MusicPlayBinder).getMusicPlayService()
            }

            override fun onServiceDisconnected(name: ComponentName?) {
                musicService = null
            }

        }
        val intent = Intent(Utils.getApp(), MusicPlayService::class.java)
        intent.action = "com.chery.media.service.MusicPlayService"
        Utils.getApp().bindService(intent, serviceConnection!!, AppCompatActivity.BIND_AUTO_CREATE)
    }

    /**
     * 设置播放列表
     */
    fun setPlayList(list: ArrayList<MusicBean>, isAdd: Boolean = false) {
        musicService?.setMusicList(list, isAdd)
        LogUtils.eTag(this.javaClass.name, "isAdd======${isAdd}")
//        if (isAdd.not()) musicService?.playMusicByIndex()
    }

    fun setCurrentPlayMusic(){
        musicService?.resetPlay()
        if (currentType.value == MusicType.USB) {
            musicService?.playMusicByIndex(currentUsbPlayMusic.value?.currentIndex ?: 0)
        } else {
            musicService?.playMusicByIndex(currentHotPlayMusic.value?.currentIndex ?: 0)
        }
    }

    /**
     * 停止音乐服务
     */
    fun stopService() {
        musicService?.stopPlay()
        Utils.getApp().unbindService(serviceConnection!!)
    }

    /**
     * 是否正在播放音乐
     */
    fun isPlaying(): Boolean {
        return when (currentType.value) {
            MusicType.BLUETOOTH -> currentBluetoothPlayMusic.value?.isPlaying == true
            MusicType.USB -> currentUsbPlayMusic.value?.isPlaying == true
            else -> currentHotPlayMusic.value?.isPlaying == true
        }
    }

    /**
     * 当前播放的音乐
     */
    fun currentPlayMusic(): MusicPlayBean? {
        return when (currentType.value) {
            MusicType.BLUETOOTH -> currentBluetoothPlayMusic.value
            MusicType.USB -> currentUsbPlayMusic.value
            else -> currentHotPlayMusic.value
        }
    }

    /**
     * 播放音乐
     */
    fun playMusic(position: Int) {
        musicService?.playMusicByIndex(index = position)
    }

    /**
     * 继续播放 or 暂停播放
     */
    fun continuePlayOrPause(){
        val isPlay = if (currentType.value == MusicType.BLUETOOTH) currentBluetoothPlayMusic.value?.isPlaying == true
        else if (currentType.value == MusicType.USB) currentUsbPlayMusic.value?.isPlaying == true
        else currentHotPlayMusic.value?.isPlaying == true
        if (isPlay) {
            playPause()
        } else {
            continuePlay()
        }
    }

    /**
     * 继续播放
     */
    private fun continuePlay(){
        if (currentType.value == MusicType.BLUETOOTH) {
            BluetoothMusicTool.operateMusic(OperateMusicType.PLAY)
        } else {
            musicService?.continuePlay()
        }
    }

    /**
     * 播放暂停
     */
    private fun playPause() {
        if (currentType.value == MusicType.BLUETOOTH) {
            BluetoothMusicTool.operateMusic(OperateMusicType.PAUSE)
        } else {
            musicService?.pausePlay()
        }
    }

    /**
     * 播放上一首
     */
    fun playPrevious() {
        if (currentType.value == MusicType.BLUETOOTH) {
            BluetoothMusicTool.operateMusic(OperateMusicType.PREVIOUS)
        } else {
            musicService?.playPrevious()
        }
    }

    /**
     * 播放下一首
     */
    fun playNext() {
        if (currentType.value == MusicType.BLUETOOTH) {
            BluetoothMusicTool.operateMusic(OperateMusicType.NEXT)
        } else {
            musicService?.playNext()
        }
    }

    /**
     * 停止播放音乐
     */
    fun stopPlay() {
        if (currentType.value == MusicType.BLUETOOTH) {
            BluetoothMusicTool.operateMusic(OperateMusicType.STOP)
        } else {
            musicService?.stopPlay()
        }
    }

    /**
     * 停止蓝牙播放音乐
     */
    fun bluetoothPlayMusic(isStop: Boolean) {
        if (isStop) {
            BluetoothMusicTool.operateMusic(OperateMusicType.PAUSE)
        } else {
            BluetoothMusicTool.operateMusic(OperateMusicType.PLAY)
        }
    }

    /**
     * 停止service播放音乐
     */
    fun servicePlayMusic(isStop: Boolean) {
        if (isStop) {
            musicService?.stopPlay()
        } else {
            musicService?.continuePlay()
        }
    }

}